class AlarmsController < ApplicationController
  before_action :set_alarm, only: [:show, :edit, :update, :destroy]
  skip_before_filter :verify_authenticity_token

  # GET /alarms
  # GET /alarms.json
  def index
    @alarmvs = Alarmv.all
    @device_id = params["id"]
    @current_admin ||=  User.find_by_token(cookies[:token]) if cookies[:token]
  end
  
  def alarmlogs
    if params["pid"]  and current_admin and  current_admin.type!=0
       @pid=current_admin.id
       @project_id=current_admin.id.to_s
       @count=Alarmlog.where(:user_id =>current_admin.id).count
       @totolpage=@count/10+1;
       @alarmlogs=Alarmlog.where(:user_id =>current_admin.id).paginate(page:params[:page],per_page:10)
    else
       @count=Alarmlog.where(:code => { '$exists'=> true }).count
       @totolpage=@count/10+1;
       @alarmlogs=Alarmlog.where(:code => { '$exists'=> true }).paginate(page:params[:page],per_page:10)
    end
    
  end
 
  def addAlarms
     contactId=params[:contactId]
     deviceId=params[:deviceId]
     sensorId=params[:sensorId]
     alarmType=params[:alarmType]
     upperBoundC=params[:upperBoundC]
     duration=params[:duration]
     lowerBoundC=params[:lowerBoundC]
     target=params[:target]
     switchVal=params[:switchVal]
     dev=Device.find(deviceId)
     userId=dev.user_id.to_s()
     alarm=Alarmv.new(contactId:contactId,deviceId:deviceId,sensorId:sensorId,alarmType:alarmType,duration:duration,upperBoundC:upperBoundC,lowerBoundC:lowerBoundC,alarmTypeDiv:upperBoundC,target:target,switchVal:switchVal)
     alarm.save
     respond_to do |format|
        format.json { render :json => {:code =>1,:msg =>"ok",:redirect_uri =>"/"} }
     end
  end
 
  def querySensorByDeviceId
     deviceId=params[:deviceId]
     device=Device.find(deviceId)
     sensors=Sensor.where(:device_id => device.id,:display =>1)
     respond_to do |format|
       format.json {render :json =>{:sensorList => sensors,:code => 0}}
     end
  end

 
  def toAddAlarms
    current_admin ||=  User.find_by_token(cookies[:token]) if cookies[:token]
    if params["pid"] and current_admin and current_admin.type!=0
      pid=params["pid"]
      prj=User.find(pid)
      @devices=prj.device
      if @devices
         @sensors= @devices[0].sensor
      end
    else
     @devices=Device.all
    end
    @contacts=Contact.all
    if @devices
       @sensors=Sensor.where(:device_id => @devices[0].id, :display =>1)
    end
  end
  
  def toUpdateAlarms
    current_admin ||=  User.find_by_token(cookies[:token]) if cookies[:token]
    if params["pid"] and  current_admin and current_admin.type!=0
      pid=params["pid"]
      prj=User.find(pid)
      @devices=prj.device
      if @devices
         @sensors= @devices[0].sensor
      end
    else
     @devices=Device.all
    end
    @contacts=Contact.all
    if @devices
       @sensors= @devices[0].sensor
    end
    @alarm=Alarm.find(params[:alarmsId])

    de= Device.find(@alarm.deviceId)
    @sensors=Sensor.where(:device_id => de.id, :display =>1)
  end
  # GET /alarms/1
  # GET /alarms/1.json
  def show
  end

  # GET /alarms/new
  def new
    @contacts = Contact.all
    @device= Device.find(params["device_id"])
    @sensors=Sensor.where(:device_id => @device.id, :display =>1)
    
  end

  # GET /alarms/1/edit
  def edit
  end

  # POST /alarms
  # POST /alarms.json
  def create
    @alarm = Alarm.new(alarm_params)

    respond_to do |format|
      if @alarm.save
        format.html { redirect_to @alarm, notice: 'Alarm was successfully created.' }
        format.json { render :show, status: :created, location: @alarm }
      else
        format.html { render :new }
        format.json { render json: @alarm.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /alarms/1
  # PATCH/PUT /alarms/1.json
  def update
    contactId=params[:mobile_alarms]
    deviceId=params[:deviceId]
    sensorId=params[:sensorId]
    alarmType=params[:alarmType]
    upperBoundC=params[:upperBoundC]
    duration=params[:duration]
    lowerBoundC=params[:lowerBoundC]
    target=params[:target]
    switchVal=params[:switchVal]
    alarmId=params[:alarmsId]
    am=Alarm.find(alarmId)
    am.update_attributes(contactId:contactId,alarmType:alarmType,upperBoundC:upperBoundC,duration:duration,lowerBoundC:lowerBoundC,target:target,switchVal:switchVal)
    respond_to do |format|
        format.json {render :json => {:code =>0,:msg =>"OK",:redirect_uri =>"/"}}
    end
  end

  # DELETE /alarms/1
  # DELETE /alarms/1.json
  def destroy
    @alarm.destroy
    respond_to do |format|
      format.json { render :json => {:code =>0,:msg =>"Alarm was successfully destroyed.",:redirect_uri =>""} }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_alarm
      @alarm = Alarm.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def alarm_params
      params.require(:alarm).permit(:name, :alarmType, :alarmType_Name, :alarmTypeDiv, :target, :target_name, :switchVal)
    end
end
